package com.wpzf.zxhc.controller;

import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.log.annotation.Log;
import com.wpzf.common.log.enums.BusinessType;
import com.wpzf.zxhc.service.JdfkStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

@RestController
@RequestMapping("/jdfk")
public class JdfkStatisticsController {
    @Autowired
    JdfkStatisticsService jdfkStatisticsService;
    @Autowired
    private ScheduledExecutorService scheduledExecutorService;

    /*获取省级行政区*/
    @Log(title = "获取省级行政区", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/xzqsheng", method = RequestMethod.GET)
    public AjaxResult getXzqSheng() {
        List<Map<String, Object>> rest = jdfkStatisticsService.getXzqSheng();
        if (rest != null) {
            return AjaxResult.success("获取省级行政区 成功", rest);
        }
        return AjaxResult.error("获取省级行政区 失败", -1);
    }

    /*获取市级行政区*/
    @Log(title = "获取市级行政区", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/xzqshi", method = RequestMethod.POST)
    public AjaxResult getXzqShi(@RequestBody Map<String, Object> parms) {
        List<Map<String, Object>> rest = jdfkStatisticsService.getXzqShi(parms);
        if (rest != null) {
            return AjaxResult.success("获取市级行政区 成功", rest);
        }
        return AjaxResult.error("获取市级行政区 失败", -1);
    }

    /*获取县级行政区*/
    @Log(title = "获取县级行政区", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/xzqxian", method = RequestMethod.POST)
    public AjaxResult getXzqXian(@RequestBody Map<String, Object> parms) {
        List<Map<String, Object>> rest = jdfkStatisticsService.getXzqXian(parms);
        if (rest != null) {
            return AjaxResult.success("获取县级行政区 成功", rest);
        }
        return AjaxResult.error("获取县级行政区 失败", -1);
    }

    /*获取判定类型*/
    @Log(title = "获取判定类型", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/pdlx", method = RequestMethod.GET)
    public AjaxResult getPdlx() {
        List<Map<String, Object>> rest = jdfkStatisticsService.getPdlx();
        if (rest != null) {
            return AjaxResult.success("获取判定类型 成功", rest);
        }
        return AjaxResult.error("获取判定类型 失败", -1);
    }

    /*获取核查结果说明*/
    @Log(title = "获取核查结果说明", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/hcjgsm", method = RequestMethod.GET)
    public AjaxResult getHcjgsm() {
        List<Map<String, Object>> rest = jdfkStatisticsService.getHcjgsm();
        if (rest != null) {
            return AjaxResult.success("获取核查结果说明 成功", rest);
        }
        return AjaxResult.error("获取核查结果说明 失败", -1);
    }

    /*获取进度反馈*/
    @Log(title = "获取进度反馈", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/jdfktable", method = RequestMethod.POST)
    public AjaxResult getJdfk(@RequestBody Map<String, Object> parms) {
        List<Map<String, Object>> rest = jdfkStatisticsService.getJdfk(parms);
        Integer cnt = jdfkStatisticsService.getJdfkCount(parms);
        if (rest != null) {
            return AjaxResult.success("获取进度反馈  成功", rest, cnt);
        }
        return AjaxResult.error("获取进度反馈  失败", -1);
    }

    /*数据导入*/
    @Log(title = "数据导入", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/uploadexcel", method = RequestMethod.POST)
    public AjaxResult uploadExcel(MultipartFile file, String xzq, String year, String month, String uptype) {
        boolean b = jdfkStatisticsService.dealWithUpData(xzq, year, month, uptype);
        if (b) {
            String sFilePath = jdfkStatisticsService.uploadFile(file);
            if (sFilePath != "") {
                scheduledExecutorService.execute(() -> {
                    jdfkStatisticsService.importDataByExcel(sFilePath, year, month);
                });
                return AjaxResult.success("数据开始导入，稍后刷新查看");
            }
        }
        return AjaxResult.error("数据导入失败");
    }
}
